﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using XiaoGang.Soft.XGWMS.COM;

namespace XiaoGang.Soft.XGWMS.UI
{
    public partial class FormAnalysis : FormBase
    {

        BLL.BLL_M012_STORAGESCAN bll = new BLL.BLL_M012_STORAGESCAN();
        private const string dataName = "销售分析";

        public FormAnalysis()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Initial
        /// </summary>
        private void Initial()
        {
            SetControlsIndex();
            InitialGridView();
            InitialControl();
        }

        /// <summary>
        /// SetControlsIndex
        /// </summary>
        protected void SetControlsIndex()
        {
            grdDetailInfo.TabStop = false;
            int intIndex = 0; intIndex++;
            rdoSales.TabIndex = intIndex; intIndex++;
            rdoMoney.TabIndex = intIndex; intIndex++;
            rdoASC.TabIndex = intIndex; intIndex++;
            rdoDESC.TabIndex = intIndex; intIndex++;
            dtpDateFrom.TabIndex = intIndex; intIndex++;
            dtpDateTo.TabIndex = intIndex; intIndex++;
            btnSearch.TabIndex = intIndex; intIndex++;
            btnClose.TabIndex = intIndex; intIndex++;
        }

        /// <summary>
        /// InitialGridView
        /// </summary>
        private void InitialGridView()
        {
            grdDetailInfo.RowHeadersVisible = false;
            grdDetailInfo.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Raised;
            grdDetailInfo.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            grdDetailInfo.ReadOnly = true;
            grdDetailInfo.ColumnHeadersHeight = 30;
            grdDetailInfo.ColumnHeadersVisible = true;
            grdDetailInfo.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            grdDetailInfo.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdDetailInfo.ReadOnly = true;
            grdDetailInfo.AllowUserToAddRows = false;
            grdDetailInfo.TabStop = false;
            grdDetailInfo.AllowUserToResizeRows = false;
            grdDetailInfo.Columns.Add("NO", "NO.");
            grdDetailInfo.Columns.Add("GOODS_ID", "编号");
            grdDetailInfo.Columns.Add("GoodsGroupName", "分类");
            grdDetailInfo.Columns.Add("GoodsName", "商品名称");
            grdDetailInfo.Columns.Add("SalesTotal", "销量");
            grdDetailInfo.Columns.Add("Sales", "销售额");
            
            grdDetailInfo.Columns["NO"].Width = 50;
            grdDetailInfo.Columns["NO"].Resizable = DataGridViewTriState.False;
            grdDetailInfo.Columns["NO"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdDetailInfo.Columns["GOODS_ID"].Width = 150;
            grdDetailInfo.Columns["GOODS_ID"].Resizable = DataGridViewTriState.False;
            grdDetailInfo.Columns["GoodsGroupName"].Width = 150;
            grdDetailInfo.Columns["GoodsGroupName"].Resizable = DataGridViewTriState.False;
            grdDetailInfo.Columns["GoodsName"].Width = 200;
            grdDetailInfo.Columns["GoodsName"].Frozen = true;
            grdDetailInfo.Columns["GoodsName"].Resizable = DataGridViewTriState.False;
            grdDetailInfo.Columns["SalesTotal"].Width = 150;
            grdDetailInfo.Columns["SalesTotal"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdDetailInfo.Columns["SalesTotal"].Resizable = DataGridViewTriState.False;
            grdDetailInfo.Columns["Sales"].Width = 150;
            grdDetailInfo.Columns["Sales"].Resizable = DataGridViewTriState.False;
        }

        /// <summary>
        /// InitialControl
        /// </summary>
        private void InitialControl()
        {
            rdoSales.Checked = true;
            rdoMoney.Checked = false;
            rdoASC.Checked = true;
            rdoDESC.Checked = false;
            dtpDateFrom.Text = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");
            dtpDateTo.Text = DateTime.Now.ToString("yyyy-MM-dd");
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            this.Cursor = Cursors.WaitCursor;
            string strDateFrom = dtpDateFrom.Text.Trim() == "" ? dtpDateFrom.Value.ToString("yyyyMMdd") : dtpDateFrom.Text.Trim().Replace("-", "").Replace("/", "");
            string strDateTo = dtpDateTo.Text.Trim() == "" ? dtpDateTo.Value.ToString("yyyyMMdd") : dtpDateTo.Text.Trim().Replace("-", "").Replace("/", "");
            int intOrderByType = 1;
            int intOrderDirection = 1;
            if (rdoSales.Checked)
            {
                intOrderByType = 2;
            }
            else
            {
                intOrderByType = 1;
            }
            if (rdoASC.Checked)
            {
                intOrderDirection = 1;
            }
            else
            {
                intOrderDirection = 2;
            }
            ReturnValue rvGoods = bll.GetAnalysis(strDateFrom, strDateTo,intOrderByType,intOrderDirection);

            if (rvGoods.IsSuccess == false)
            {
                Method.MessageShow(rvGoods.ErrorID, dataName);
                this.Cursor = Cursors.Default;
                return;
            }
            grdDetailInfo.Rows.Clear();

            //显示详细
            foreach (DataRow row in rvGoods.ResultDataSet.Tables[0].Rows)
            {
                grdDetailInfo.Rows.Add(
                    (grdDetailInfo.Rows.Count + 1).ToString(),
                    row["GOODS_ID"].ToString().Trim(),
                    row["GoodsGroupName"].ToString().Trim(),
                    row["GoodsName"].ToString().Trim(),
                    row["SalesTotal"].ToString().Trim(),
                    row["Sales"].ToString().Trim());
                //TotalSales += decimal.Parse(row["AllPrice"].ToString().Trim());
            }
            this.Cursor = Cursors.Default;

            grpBillDetailInfo.Text = String.Format("销售记录 共{0}条数据 最多显示前50条数据", grdDetailInfo.Rows.Count);
        }

        private void FormAnalysis_Load(object sender, EventArgs e)
        {
            Initial();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormAnalysis_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F2:
                    btnSearch_Click(sender, e);
                    break;
                case Keys.Escape:
                    btnClose_Click(sender, e);
                    break;
            }
        }
    }
}
